﻿// <copyright file="IdSpec.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-03-24</date>
// <summary>OpenSocial IdSpec</summary>

namespace opensocial
{
    using System.Globalization;
    using System.Windows.Browser;
    using Synergist;

    /// <summary>
    /// OpenSocial IdSpec
    /// </summary>
    public class IdSpec
    {
        /// <summary>
        /// the base ScriptObject
        /// </summary>
        private ScriptObject idspec;

        /// <summary>
        /// Initializes a new instance of the IdSpec class.
        /// </summary>
        public IdSpec()
        {
            if (HtmlPage.IsEnabled)
            {
                this.idspec = HtmlPage.Window.Eval("new opensocial.IdSpec()") as ScriptObject;
            }
        }

        /// <summary>
        /// Initializes a new instance of the IdSpec class.
        /// </summary>
        /// <param name="scriptObject">a JavaScript IdSpec object</param>
        public IdSpec(ScriptObject scriptObject)
        {
            this.idspec = scriptObject;
        }

        /// <summary>
        /// Gets the base JavaScript object
        /// </summary>
        public ScriptObject ScriptObject
        {
            get
            {
                return this.idspec;
            }
        }

        /// <summary>
        /// Gets or sets the User Id
        /// </summary>
        public string UserId
        {
            get
            {
                return this.GetField("opensocial.IdSpec.Field.USER_ID") as string;
            }

            set
            {
                this.SetField("opensocial.IdSpec.Field.USER_ID", value);
            }
        }

        /// <summary>
        /// Gets or sets the group Id
        /// </summary>
        public string GroupId
        {
            get
            {
                return this.GetField("opensocial.IdSpec.Field.GROUP_ID") as string;
            }

            set
            {
                this.SetField("opensocial.IdSpec.Field.GROUP_ID", value);
            }
        }

        /// <summary>
        /// Gets or sets the network distance
        /// </summary>
        public int NetworkDistance
        {
            get
            {
                object distance = (int) this.GetField("opensocial.IdSpec.Field.NETWORK_DISTANCE");

                return (int)distance;
            }

            set
            {
                this.SetField("opensocial.IdSpec.Field.NETWORK_DISTANCE", value);
            }
        }

        /// <summary>
        /// Gets the displayName for the IdSpec
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification = "This is a complex function.")]
        public string DisplayName
        {
            get
            {
                if (!HtmlPage.IsEnabled)
                {
                    return "Owner Friends";
                }

                if (this.UserId == "opensocial.IdSpec.PersonId.VIEWER".Eval() && this.NetworkDistance == 0)
                {
                    return "Viewer";
                }

                if (this.UserId == "opensocial.IdSpec.PersonId.VIEWER".Eval() && this.NetworkDistance == int.MinValue && this.GroupId == null)
                {
                    return "Viewer";
                }

                if (this.UserId == "opensocial.IdSpec.PersonId.VIEWER".Eval() && this.NetworkDistance == int.MinValue && this.GroupId == "opensocial.IdSpec.GroupId.SELF".Eval())
                {
                    return "Viewer";
                }

                if (this.UserId == "opensocial.IdSpec.PersonId.OWNER".Eval() && this.NetworkDistance == 0)
                {
                    return "Owner";
                }

                if (this.UserId == "opensocial.IdSpec.PersonId.OWNER".Eval() && this.NetworkDistance == int.MinValue && this.GroupId == null)
                {
                    return "Owner";
                }

                if (this.UserId == "opensocial.IdSpec.PersonId.OWNER".Eval() && this.NetworkDistance == int.MinValue && this.GroupId == "opensocial.IdSpec.GroupId.SELF".Eval())
                {
                    return "Owner";
                }

                if (this.UserId == "opensocial.IdSpec.PersonId.VIEWER".Eval() && this.NetworkDistance == 1)
                {
                    return "Viewer Friends";
                }

                if (this.UserId == "opensocial.IdSpec.PersonId.VIEWER".Eval() && this.NetworkDistance == int.MinValue && this.GroupId == "opensocial.IdSpec.GroupId.FRIENDS".Eval())
                {
                    return "Viewer Friends";
                }

                if (this.UserId == "opensocial.IdSpec.PersonId.OWNER".Eval() && this.NetworkDistance == 1)
                {
                    return "Owner Friends";
                }

                if (this.UserId == "opensocial.IdSpec.PersonId.OWNER".Eval() && this.NetworkDistance == int.MinValue && this.GroupId == "opensocial.IdSpec.GroupId.FRIENDS".Eval())
                {
                    return "Owner Friends";
                }

                return string.Format(CultureInfo.CurrentCulture, "User {0}", this.UserId);
            }
        }

        /// <summary>
        /// Gets the properties for the object
        /// </summary>
        /// <returns>a string listing the non-null properties and their values.</returns>
        public override string ToString()
        {
            return Synergist.Utility.GetProperties(this);
        }

        /// <summary>
        /// Set a field
        /// </summary>
        /// <param name="name">the field name</param>
        /// <param name="value">the field value</param>
        private void SetField(string name, object value)
        {
            if (HtmlPage.IsEnabled)
            {
                this.idspec.Invoke("setField", name.Eval(), value);
            }
        }

        /// <summary>
        /// Get a field
        /// </summary>
        /// <param name="name">the field name</param>
        /// <returns>the field value</returns>
        private object GetField(string name)
        {
            if (HtmlPage.IsEnabled)
            {
                return this.idspec.Invoke("getField", name.Eval());
            }

            return null;
        }
    }
}
